//
//  HMStatusOriginalView.swift
//  Weibo22
//
//  Created by Apple on 16/12/4.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit
import YYText

//  原创微博视图
class HMStatusOriginalView: UIView {

    //  记录原创微博的底部约束
    var originalViewBottomContraint: Constraint?
    
    //  所需要的视图模型(ViewModel)
    var statusViewModel: HMStatusViewModel? {
        didSet {
            
            if let imageUrl = statusViewModel?.status?.user?.profile_image_url {
                userImageView.sd_setImage(with: URL(string: imageUrl), placeholderImage: UIImage(named: "avatar_default_big"))
            }
            
            screenNameLabel.text = statusViewModel?.status?.user?.screen_name
            
//            contentLabel.text = statusViewModel?.status?.text
            //  因为是图文混排需要使用富文本
            contentLabel.attributedText = statusViewModel?.originalAttributedString
            
            mbrankImageView.image = statusViewModel?.mbrankImage
            
            verifiedTypeImageView.image = statusViewModel?.verifiedTypeImage
            
            //  来源数据绑定
            sourceLabel.text = statusViewModel?.sourceContent
            //  设置时间
            timeLabel.text = statusViewModel?.timeContent
            
            //  卸载上次记录的约束
            originalViewBottomContraint?.uninstall()
            
            //  判断原创微博是否有配图
            if let picUrls = statusViewModel?.status?.pic_urls, picUrls.count > 0  {
                //  有配图
                pictureView.isHidden = false
                //  更新约束
                self.snp_updateConstraints(closure: { (make) in
                    originalViewBottomContraint = make.bottom.equalTo(pictureView).offset(StatusItemMargin).constraint
                })
                //  设置数据源
                pictureView.picUrls = picUrls
                
            } else {
                //  没有配图
                pictureView.isHidden = true
                //  更新约束
                self.snp_updateConstraints(closure: { (make) in
                    originalViewBottomContraint = make.bottom.equalTo(contentLabel).offset(StatusItemMargin).constraint
                })
            }
            
            
            
        }
    }
    
    //  MARK: -- 懒加载
    //  用户头像
    private lazy var userImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //  认证类型等级图片
    private lazy var verifiedTypeImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    //  昵称
    private lazy var screenNameLabel: UILabel = {
        let label = UILabel(fontSize: 14, textColor: UIColor.darkGray)
        label.text = "物理大法"
        return label
    }()
    //  会员等级图片
    private lazy var mbrankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    //  时间
    private lazy var timeLabel: UILabel =  {
        let label = UILabel(fontSize: 12, textColor: UIColor.orange)
        
        label.text = "刚刚"
        return label
    }()
    
    //  来源
    private lazy var sourceLabel: UILabel =  {
        let label = UILabel(fontSize: 12, textColor: UIColor.lightGray)
        label.text = "来自: 星星的你"
        return label
    }()
    
    //  微博内容
    private lazy var contentLabel: YYLabel =  {
//        let label = UILabel(fontSize: 14, textColor: UIColor.darkGray)
        let label = YYLabel()
         //创建一个文本行位置修改类
        let modifier = YYTextLinePositionSimpleModifier()
        modifier.fixedLineHeight = 22;
        
        label.linePositionModifier = modifier;
        
        //  最大换行宽度
        label.preferredMaxLayoutWidth = ScreenWidth - 2 * StatusItemMargin
        label.text = "来自: 星星的你来自: 星星的你来自: 星星的你来自: 星星的你来自: 星星的你来自: 星星的你"
        
        //  多行显示
        label.numberOfLines = 0
        return label
    }()
    
    //  配图
    private lazy var pictureView: HMStatusPictureView = {
        let pictureView = HMStatusPictureView()
        pictureView.backgroundColor = self.backgroundColor
        return pictureView
    }()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        //  设置背景色
        backgroundColor = UIColor.white
        
        addSubview(userImageView)
        addSubview(verifiedTypeImageView)
        addSubview(screenNameLabel)
        addSubview(mbrankImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        userImageView.snp_makeConstraints { (make) in
            make.top.equalTo(self).offset(StatusItemMargin)
            make.leading.equalTo(self).offset(StatusItemMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        verifiedTypeImageView.snp_makeConstraints { (make) in
            make.centerX.equalTo(userImageView.snp_trailing)
            make.centerY.equalTo(userImageView.snp_bottom)
        }
        
        screenNameLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(userImageView.snp_trailing).offset(StatusItemMargin)
            make.top.equalTo(userImageView)
        }
        
        mbrankImageView.snp_makeConstraints { (make) in
            make.leading.equalTo(screenNameLabel.snp_trailing).offset(StatusItemMargin)
            make.top.equalTo(screenNameLabel)
        }
        
        timeLabel.snp_makeConstraints { (make) in
            make.bottom.equalTo(userImageView)
            make.leading.equalTo(screenNameLabel)
        }
        
        sourceLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(timeLabel.snp_trailing).offset(StatusItemMargin)
            make.bottom.equalTo(timeLabel)
        }
        
        contentLabel.snp_makeConstraints { (make) in
            make.top.equalTo(userImageView.snp_bottom).offset(StatusItemMargin)
            make.leading.equalTo(userImageView)
            
            make.width.equalTo(ScreenWidth - 2 * StatusItemMargin)
        }
        
        pictureView.snp_makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusItemMargin)
            make.leading.equalTo(contentLabel)
        }
        
        //  关键代码
        self.snp_makeConstraints { (make) in
            originalViewBottomContraint = make.bottom.equalTo(pictureView).offset(StatusItemMargin).constraint
        }
        //  设置高亮文字点击执行的闭包
        
        //  containerView: 容器视图-> YYLabel
        //  attributedStr        : YYLabel显示的富文本
        //  range       : 点击高亮文字的范围
        //  rect         : 点击高亮文字的区域
        contentLabel.highlightTapAction = { (containerView, attributedStr, range, rect) in
            let result = (attributedStr.string as NSString).substring(with: range)
            print(result)
        }
        
    }

}
